package com.vm.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.commons.StringUtil;
import com.vm.commons.VMConst;
import com.vm.model.Soft;
import com.vm.utils.MyDisplayImageOptions;
import com.wwtscapp.apk.R;

public class CategoryHotAdapter extends BaseAdapter {

	private Context mContext;
	private List<Soft> listSofts;

	/**
	 * 
	 */
	public CategoryHotAdapter(Context context, List<Soft> lists) {
		this.mContext = context;
		this.listSofts = lists;
	}

	@Override
	public int getCount() {
		return listSofts.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.category_item, null);
			viewHolder = new ViewHolder();

			/**
			 * 获取各个需要显示属性的控件
			 */
			viewHolder.tv_softName = (TextView) convertView
					.findViewById(R.id.tv_softName);
			viewHolder.iv_softImage = (ImageView) convertView
					.findViewById(R.id.iv_softImage);
			viewHolder.tv_softSize = (TextView) convertView
					.findViewById(R.id.tv_softSize);
			viewHolder.rb_star = (RatingBar) convertView
					.findViewById(R.id.rb_star);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		Soft soft = listSofts.get(position);

		viewHolder.tv_softName.setTag(soft.getId());
		if (soft.getName() != null) {
			viewHolder.tv_softName.setText(StringUtil.getShotName(soft
					.getName()));
		}
		if (soft.getSize() != null) {
			viewHolder.tv_softSize
					.setText(Float.toString(soft.getSize()) + "M");
		}
		if (soft.getStar() != null) {
			float fstar = soft.getStar();
			viewHolder.rb_star.setRating(fstar / 2);
		}
		String url = VMConst.URL + soft.getImageUrl();
		ImageLoader.getInstance().displayImage(url, viewHolder.iv_softImage,
				MyDisplayImageOptions.getInstance());

		soft = null;
		return convertView;

	}

	static class ViewHolder {
		TextView tv_softName;
		ImageView iv_softImage;
		TextView tv_softSize;
		RatingBar rb_star;
	}

}
